﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;

namespace Seagull.Controls
{
    public class MapHelper
    {
        public const int MarkerRadius = 15;

        public static Point ToPointOnMap(
            Point pointOnControl, Point pictureSize, Point mapSize)
        {
            var x = (int)Math.Round(pointOnControl.X * 1.0 / pictureSize.X * mapSize.X);
            var y = (int)Math.Round(pointOnControl.Y * 1.0 / pictureSize.Y * mapSize.Y);
            return new Point(x, y);
        }

        public static Point FromPointOnMap(
            Point pointOnMap, Size pictureSize, Size mapSize)
        {
            var x = (int)Math.Round(pointOnMap.X * 1.0 / mapSize.Width * pictureSize.Width);
            var y = (int)Math.Round(pointOnMap.Y * 1.0 / mapSize.Height * pictureSize.Height);
            return new Point(x, y);
        }

        public static Point Transform(Point point, Size fromSize, Size toSize)
        {
            var x = (int)Math.Round(point.X * 1.0 / fromSize.Width * toSize.Width);
            var y = (int)Math.Round(point.Y * 1.0 / fromSize.Height * toSize.Height);
            return new Point(x, y);
        }

        public static void DrawMarker(Point pointOnControl, Graphics graphics)
        {
            graphics.FillEllipse(new SolidBrush(Color.White),
                (float)(pointOnControl.X - MarkerRadius / 2.0),
                (float)(pointOnControl.Y - MarkerRadius / 2.0),
                MarkerRadius, MarkerRadius);
        }

        public static OvalShape DrawMarker(Point pointOnControl, ShapeContainer parent)
        {
            var markerRadius = MarkerRadius;
            return DrawMarker(pointOnControl, parent, markerRadius);
        }

        public static OvalShape DrawMarker(Point pointOnControl, ShapeContainer parent, int markerRadius)
        {
            var oval = new OvalShape(
                (int) (pointOnControl.X - markerRadius/2.0),
                (int) (pointOnControl.Y - markerRadius/2.0),
                markerRadius, markerRadius)
            {
                FillColor = Color.White,
                FillStyle = FillStyle.Solid,
                BackColor = Color.White,
                BackStyle = BackStyle.Opaque,
                Parent = parent
            };
            var originalParentSize = parent.Size;
            parent.Resize += (sender, args) =>
            {
                oval.Location = Point.Subtract(
                    Transform(pointOnControl, originalParentSize, parent.Size),
                    new Size((int) (markerRadius/2.0), (int) (markerRadius/2.0)));
            };
            return oval;
        }
    }
}
